package main

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"

	mysqlproxy "gitee.com/xuender/mysql-proxy"
	"gitee.com/xuender/mysql-proxy/cmd"
	"gitee.com/xuender/mysql-proxy/pb"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xuender/oils/base"
	"github.com/xuender/oils/logs"
)

func main() {
	configFile := filepath.Join(".", "proxy.toml")
	flag.Usage = usage

	flag.StringVar(&configFile, "config", configFile, "配置文件")
	flag.Parse()

	if mysqlproxy.Version != "" {
		gin.SetMode(gin.ReleaseMode)
		// logs.RotateLog("/var", "tmp")
		logs.SetInfoLevel()
	}

	config := pb.NewConfig(configFile)
	base.Panic(config.Check())
	service := cmd.InitProxy(config)
	// defer service.Close()

	service.Run()
}

func usage() {
	fmt.Fprintf(flag.CommandLine.Output(), "proxy [%s]\n\n", mysqlproxy.Version)
	fmt.Fprintf(flag.CommandLine.Output(), "mysql proxy.\n\n")
	fmt.Fprintf(flag.CommandLine.Output(), "usage: %s\n", os.Args[0])
	flag.PrintDefaults()
	fmt.Fprintf(flag.CommandLine.Output(), "\nDate: %s\nSum: %s\n", mysqlproxy.BuildTime, mysqlproxy.GitCommit)
}
